package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import org.springframework.stereotype.Service;

@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements AddressBookService {
    @Override
    public void updDefault(AddressBook addressBook) {
        AddressBook addressBook1 = this.getById(addressBook.getId());
        Long userId = addressBook1.getUserId();
        LambdaUpdateWrapper<AddressBook> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(userId!=null,AddressBook::getUserId,userId)
                .set(AddressBook::getIsDefault,0);
        this.update(lambdaUpdateWrapper);


        LambdaUpdateWrapper<AddressBook> lambdaUpdateWrapper1 = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper1.eq(addressBook.getId()!=null,AddressBook::getId,addressBook.getId());
        addressBook1.setIsDefault(1);
        this.update(addressBook1,lambdaUpdateWrapper1);
    }

    @Override
    public void updateAddressBookById(AddressBook addressBook) {
        LambdaUpdateWrapper<AddressBook> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(addressBook.getId()!=null,AddressBook::getId,addressBook.getId())
                .set(AddressBook::getCityCode,addressBook.getCityCode())
                .set(AddressBook::getConsignee,addressBook.getConsignee())
                .set(AddressBook::getCityName,addressBook.getCityName())
                .set(AddressBook::getDetail,addressBook.getDetail())
                .set(AddressBook::getDistrictCode,addressBook.getDistrictCode())
                .set(AddressBook::getDistrictName,addressBook.getDistrictName())
                .set(AddressBook::getLabel,addressBook.getLabel())
                .set(AddressBook::getPhone,addressBook.getPhone())
                .set(AddressBook::getSex,addressBook.getSex())
                .set(AddressBook::getProvinceCode,addressBook.getProvinceCode())
                .set(AddressBook::getProvinceName,addressBook.getProvinceName());
                this.update(lambdaUpdateWrapper);

    }

    @Override
    public AddressBook getDefaultAddressByUserId() {
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(userId!=null,AddressBook::getUserId,userId)
                .eq(AddressBook::getIsDefault,1);
        AddressBook addressBook = this.getOne(lambdaQueryWrapper);
        return addressBook;
    }
}
